import { _decorator, Animation, Component } from 'cc';
import { autoComp, rootNode } from '../Manager/BaseManager';
const { ccclass } = _decorator;

@ccclass('AutoAnimation')
export class AutoAnimation extends Component {
  @autoComp(Animation, rootNode)
  anim: Animation;

  aniLock: boolean = false;
  protected onEnable(): void {
    this.anim.stop();
    this.anim.defaultClip = null;
    if (this.aniLock) return;
    this.anim.play(this.anim.clips[0].name);
  }

  stopAnimation() {
    this.aniLock = true;
    this.anim.stop();
    this.anim.defaultClip = null;
  }

  resumeAnimation() {
    this.aniLock = false;
    this.onEnable();
  }

  autoDestory() {
    this.node.removeFromParent();
    this.node.destroy();
  }
}
